package com.partners.controller.Page;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.partners.annotation.Auths;
import com.partners.dao.dto.UserInfoOutputDTO;
import com.partners.service.UserService;
import com.partners.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @author liangyongle
 * @date 2018-12-14
 */
@Controller
@RequestMapping("loginPage")
@Slf4j
public class LoginPageController {

    @Resource
    private UserService userService;

    @GetMapping("index")
    @Auths
    public ModelAndView fanInfo(HttpSession session,@RequestParam(value="bind", required=false) Integer id){
        id = id!=null?id:0;
        UserInfoOutputDTO userInfo = userService.getUserInfo(id);
        if(userInfo != null){
            Object fansInfo = session.getAttribute("openId");
            JSONObject jsonObject = JSON.parseObject(JsonUtils.toJson(fansInfo));
            String openId = jsonObject.getString("openid");
            Integer uid = jsonObject.getInteger("userId");
            if(uid != 0){
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletResponse response = requestAttributes.getResponse();
                String  redirectUrl = "/userPage/usercenter";
                try {
                    response.sendRedirect(redirectUrl);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            ModelAndView mav = new ModelAndView("show/login");
            mav.addObject("id",id);
            mav.addObject("username",userInfo.getName());
            mav.addObject("phone",userInfo.getPhone().toString());
            mav.addObject("role_name",userInfo.getRoleName());
            mav.addObject("openid",openId);
            return mav;
        }else{
            ModelAndView mav = new ModelAndView("show/error");
            mav.addObject("code","406");
            mav.addObject("msg","找不到该用户");
            return mav;
        }
    }

    /**
    * 报错页面
    * */
    @GetMapping("error/{code}")
    public ModelAndView error(@PathVariable("code") Integer code){
        ModelAndView mav = new ModelAndView("show/error");
        String msg= "";
        switch(code){
            case 405:msg ="二维码已过期";break;
            default: msg = "无效链接";break;
        }
        mav.addObject("code",code);
        mav.addObject("msg",msg);
        return mav;
    }

}
